package com.example.flight.service;

import com.alibaba.fastjson.JSONObject;
import com.example.flight.generate.Source2Dest;
import com.example.flight.generate.TongChengCity;
import com.example.flight.generate.TongChengFlight;
import com.example.flight.util.DateUtil;
import com.google.common.collect.Lists;
import org.checkerframework.checker.units.qual.A;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;
import java.text.ParseException;
import java.util.*;

/**
 * @Author: leo
 * @date: 2021/7/5
 */
public class TongChengService {

    public static  Map<String,String> getTongChengCityMap(){
        Map<String,String> map = new HashMap<>();
        map.put("南宁" , "NNG");
        map.put("济南" , "TNA");
        map.put("昆明" , "KMG");
        map.put("临沂" , "LYI");
        map.put("赣州" , "KOW");
        map.put("宁波" , "NGB");
        map.put("重庆" , "CKG");
        map.put("石家庄" , "SJW");
        map.put("贵阳" , "KWE");
        map.put("长春" , "CGQ");
        map.put("哈尔滨" , "HRB");
        map.put("鞍山" , "AOG");
        map.put("合肥" , "HFE");
        map.put("天津" , "TSN");
        map.put("温州" , "WNZ");
        map.put("青岛" , "TAO");
        map.put("杭州" , "HGH");
        map.put("成都" , "CTU");
        map.put("上海" , "SHA");
        map.put("沈阳" , "SHE");
        map.put("南通" , "NTG");
        map.put("武汉" , "WUH");
        map.put("深圳" , "SZX");
        map.put("银川" , "INC");
        map.put("西安" , "SIA");
        map.put("郑州" , "CGO");
        map.put("太原" , "TYN");
        map.put("北京" , "BJS");
        map.put("南京" , "NKG");
        map.put("鞍山" , "AOG");
        map.put("北京" , "PEK");
        map.put("成都" , "CTU");
        map.put("贵阳" , "KWE");
        map.put("哈尔滨" , "HRB");
        map.put("杭州" , "HGH");
        map.put("合肥" , "HFE");
        map.put("济南" , "TNA");
        map.put("昆明" , "KMG");
        map.put("南京" , "NKG");
        map.put("南宁" , "NNG");
        map.put("上海" , "SHA");
        map.put("深圳" , "SZX");
        map.put("沈阳" , "SHE");
        map.put("石家庄" , "SJW");
        map.put("太原" , "TYN");
        map.put("天津" , "TSN");
        map.put("青岛" , "TAO");
        map.put("温州" , "WNZ");
        map.put("武汉" , "WUH");
        map.put("西安" , "XIY");
        map.put("银川" , "INC");
        map.put("长春" , "CGQ");
        map.put("郑州" , "CGO");
        map.put("重庆" , "CKG");
        return map;
    }


    public static void main(String[] args) throws Exception {
        /*ArrayList<String> cityList = Lists.newArrayList("鞍山", "北京", "成都", "贵阳", "哈尔滨", "杭州", "合肥", "济南", "昆明", "南京", "南宁", "上海", "深圳", "沈阳", "石家庄", "太原", "天津", "温州", "武汉", "西安", "银川", "长春", "郑州", "重庆");
        cityList.forEach(e->{
            JSONObject o = null;
            try {
                o = findCityCode(e);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            JSONObject obj = o.getJSONObject("obj");
            TongChengCity tongChengCity = obj.toJavaObject(TongChengCity.class);
            System.out.println("map.put(\""+e+"\" , \""+tongChengCity.getBody().getRl().get(0).getCode()+"\");");


        });*/
        List<Source2Dest> list = source2DestList();
        list.forEach(e->{
            JSONObject flightInfo = null;
            try {
                flightInfo = findFlightInfo(e.getDCity(), e.getACity());
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            TongChengFlight tongChengFlight = flightInfo.toJavaObject(TongChengFlight.class);
            TongChengFlight.DataBean data = tongChengFlight.getData();
            while (data==null){
                try {
                    flightInfo = findFlightInfo(e.getDCity(), e.getACity());
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                tongChengFlight = flightInfo.toJavaObject(TongChengFlight.class);
                data = tongChengFlight.getData();
            }
            Optional<TongChengFlight.DataBean.FltsBean> first = data.getFlts().stream().filter(d -> {
                try {
                    return DateUtil.isEffectiveDate(d.getDepTime());
                } catch (ParseException ex) {
                    ex.printStackTrace();
                }
                return false;
            }).findFirst();
            if (first.isPresent()){
                TongChengFlight.DataBean.FltsBean fltsBean = first.get();
                System.out.println(e.getDCity()+"$"+e.getACity()+"$"+fltsBean.getLowestDiscount()+"$"+fltsBean.getLowestFare());

            }else {
                System.out.println(e.getDCity()+"$"+e.getACity()+"$"+"空"+"$空");

            }
        });
    }

    public static List<Source2Dest> source2DestList(){
        List<Source2Dest> list = new ArrayList<>();
        Source2Dest info ;
//        info = new Source2Dest("鞍山","成都");
//        list.add(info);
//        info = new Source2Dest("北京","成都");
//        list.add(info);
//        info = new Source2Dest("北京","青岛");
//        list.add(info);
//        info = new Source2Dest("北京","深圳");
//        list.add(info);
//        info = new Source2Dest("成都","北京");
//        list.add(info);
//        info = new Source2Dest("成都","哈尔滨");
//        list.add(info);
//        info = new Source2Dest("成都","上海");
//        list.add(info);
//        info = new Source2Dest("成都","深圳");
//        list.add(info);
//        info = new Source2Dest("成都","太原");
//        list.add(info);
//        info = new Source2Dest("贵阳","深圳");
//        list.add(info);
//        info = new Source2Dest("哈尔滨","深圳");
//        list.add(info);
//        info = new Source2Dest("杭州","深圳");
//        list.add(info);
//        info = new Source2Dest("合肥","深圳");
//        list.add(info);
//        info = new Source2Dest("济南","深圳");
//        list.add(info);
//        info = new Source2Dest("昆明","深圳");
//        list.add(info);
//
//
//        info = new Source2Dest("南京",	"深圳");
//        list.add(info);
//        info = new Source2Dest("南宁",	"深圳");
//        list.add(info);
//        info = new Source2Dest("上海",	"深圳");
//        list.add(info);
//        info = new Source2Dest("深圳",	"北京");
//        list.add(info);
//        info = new Source2Dest("深圳",	"成都");
//        list.add(info);
//        info = new Source2Dest("深圳",	"赣州");
//        list.add(info);
//        info = new Source2Dest("深圳",	"贵阳");
//        list.add(info);
//        info = new Source2Dest("深圳",	"杭州");
//        list.add(info);
//        info = new Source2Dest("深圳",	"合肥");
//        list.add(info);
//        info = new Source2Dest("深圳",	"济南");
//        list.add(info);
//        info = new Source2Dest("深圳",	"昆明");
//        list.add(info);
//        info = new Source2Dest("深圳",	"临沂");
//        list.add(info);
//        info = new Source2Dest("深圳",	"南京");
//        list.add(info);
//        info = new Source2Dest("深圳",	"南宁");
//        list.add(info);
//        info = new Source2Dest("深圳",	"南通");
//        list.add(info);
//        info = new Source2Dest("深圳",	"宁波");
//        list.add(info);
//        info = new Source2Dest("深圳",	"青岛");
//        list.add(info);
//        info = new Source2Dest("深圳",	"上海");
//        list.add(info);
        info = new Source2Dest("深圳",	"沈阳");
        list.add(info);
        info = new Source2Dest("深圳",	"石家庄");
        list.add(info);
        info = new Source2Dest("深圳",	"太原");
        list.add(info);
        info = new Source2Dest("深圳",	"天津");
        list.add(info);
        info = new Source2Dest("深圳",	"温州");
        list.add(info);
        info = new Source2Dest("深圳",	"武汉");
        list.add(info);
        info = new Source2Dest("深圳",	"西安");
        list.add(info);
        info = new Source2Dest("深圳",	"银川");
        list.add(info);
        info = new Source2Dest("深圳",	"长春");
        list.add(info);
        info = new Source2Dest("深圳",	"郑州");
        list.add(info);
        info = new Source2Dest("深圳",	"重庆");
        list.add(info);
        info = new Source2Dest("沈阳",	"成都");
        list.add(info);
        info = new Source2Dest("沈阳",	"深圳");
        list.add(info);
        info = new Source2Dest("南京",	"深圳");
        list.add(info);
        info = new Source2Dest("石家庄","深圳");
        list.add(info);
        info = new Source2Dest("太原",	"深圳");
        list.add(info);
        info = new Source2Dest("天津",	"深圳");
        list.add(info);
        info = new Source2Dest("温州",	"深圳");
        list.add(info);
        info = new Source2Dest("武汉",	"深圳");
        list.add(info);
        info = new Source2Dest("西安",	"深圳");
        list.add(info);
        info = new Source2Dest("银川",	"深圳");
        list.add(info);
        info = new Source2Dest("银川",	"重庆");
        list.add(info);
        info = new Source2Dest("长春",	"成都");
        list.add(info);
        info = new Source2Dest("长春",	"深圳");
        list.add(info);
        info = new Source2Dest("郑州",	"深圳");
        list.add(info);
        info = new Source2Dest("重庆",	"深圳");
        list.add(info);
        info = new Source2Dest("重庆",	"银川");
        list.add(info);
        return list;
    }

    public static JSONObject findCityCode(String keyWord) throws IOException {
        String url = "https://tmc.ly.com/tmc/api/cityportsearch/search-flight";
        // 获取请求连接
        Connection con = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true);

        String jsonContent = "{\n" +
                "\"keyWord\": "+"\""+keyWord+"\""+",\n" +
                "    \"abTest\": \"a\",\n" +
                "    \"resTp\": 1,\n" +
                "    \"plat\": \"2\",\n" +
                "    \"responseFormat\": \"json\",\n" +
                "    \"token\": \"TMCRkUzRjBBMDc3RjdENzREOURGMURCRjAzMzdBREQ3OURDNjBCQzkyOTJDOTIyNTZEMjQ4NkEwNDVCMzI0QkY1OA==\"\n" +
                "}";
//
//        jsonContent = "{\n" +
//                "    \"keyWord\": \"鞍山\",\n" +
//                "    \"abTest\": \"a\",\n" +
//                "    \"resTp\": 1,\n" +
//                "    \"plat\": \"2\",\n" +
//                "    \"responseFormat\": \"json\",\n" +
//                "    \"token\": \"TMCRkUzRjBBMDc3RjdENzREOURGMURCRjAzMzdBREQ3OURDNjBCQzkyOTJDOTIyNTZEMjQ4NkEwNDVCMzI0QkY1OA==\"\n" +
//                "}";

        con.requestBody(jsonContent);
        // 插入cookie（头文件形式）
        String cookie = "tmcLanguage=zh; tmcCode=tctmc; Hm_lvt_ee805d81eabde6752eabaa82c79a7b7f=1625465700; Hm_lpvt_ee805d81eabde6752eabaa82c79a7b7f=1625465700; _dx_uzZo5y=7ee476295c086dffd527cbbd122057a737c122b17e853d3c63ed46ebe9dffe99c8239e4b; _dx_app_bc4b3ca6ae27747981b43e9f4a6aa769=60e2a36bW3rKVVDzbGb1yxdXdts9syvZwDT6G3J1; _dx_captcha_vid=A642A602B5BE69F8A9CC61EC87CDCF08132E5146B04AA2D466F4FF5C9790B610ABA5FF7950788EDCBFF112C7F8C9ADDF47C6D6DE4E70233B403A7C1F7DDC17FB9322B49818F75F15A62575D4732AA12D; tmcToken=TMCRkUzRjBBMDc3RjdENzREOURGMURCRjAzMzdBREQ3OURDNjBCQzkyOTJDOTIyNTZEMjQ4NkEwNDVCMzI0QkY1OA%3D%3D; tokenTime=1625465715655; xingchengKey=; depCodeName=%E4%B8%8A%E6%B5%B7; arrCodeName=%E5%8C%97%E4%BA%AC; depCodeAndName=%E4%B8%8A%E6%B5%B7*SHA*N; arrCodeAndName=%E5%8C%97%E4%BA%AC*PEK*N; depDate=2021-07-15; hotelDataNew=%7B%22cityId%22%3A%220101%22%2C%22cityName%22%3A%22%E5%8C%97%E4%BA%AC%22%2C%22startDate%22%3A%222021-07-05%22%2C%22endDate%22%3A%222021-07-06%22%7D";
        con.header("cookie", cookie);
        con.header(
                "user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
        Document doc = con.header("Content-Type", "application/json").post();
        Element body = doc.body();
        JSONObject json = JSONObject.parseObject(body.text());
        return json;
    }

    public static JSONObject findFlightInfo(String dCity,String aCity) throws IOException {
        String url = "https://tmc.ly.com/tmc/api/domesticaircontroller/searchFlightList-flight";
        // 获取请求连接
        Connection con = Jsoup.connect(url).ignoreContentType(true).ignoreHttpErrors(true);
        String jsonContent = "{\n" +
                "    \"depDate\": \"2021-07-15\",\n" +
                "    \"channelResource\": \"1\",\n" +
                "    \"token\": \"TMCNTg4RUNDOTNBMDRBMUM3Q0Q3Mjk5RjUwMEU4MTRFRTY0QjA5NDExMTE3N0I5MzBCNkFDM0JDNUJCRTVGMkYzNw==\",\n" +
                "    \"passengers\": [\n" +
                "        {\n" +
                "            \"passengerId\": 1001695234,\n" +
                "            \"passengerName\": \"陈凌\"\n" +
                "        }\n" +
                "    ],\n" +
                "    \"depCode\": "+"\""+getTongChengCityMap().get(dCity)+"\""+",\n" +
                "    \"isDepCityType\": \"N\",\n" +
                "    \"arrCode\": "+"\""+getTongChengCityMap().get(aCity)+"\""+",\n" +
                "    \"isArrCityType\": \"N\",\n" +
                "    \"isOfficial\": \"1\",\n" +
                "    \"approvalNo\": \"\"\n" +
                "}";
        con.requestBody(jsonContent);
        // 插入cookie（头文件形式）
        String cookie = "tmcLanguage=zh; tmcCode=tctmc; Hm_lvt_ee805d81eabde6752eabaa82c79a7b7f=1625465700; _dx_uzZo5y=7ee476295c086dffd527cbbd122057a737c122b17e853d3c63ed46ebe9dffe99c8239e4b; _dx_app_bc4b3ca6ae27747981b43e9f4a6aa769=60e2a36bW3rKVVDzbGb1yxdXdts9syvZwDT6G3J1; xingchengKey=; depDate=2021-07-15; hotelDataNew=%7B%22cityId%22%3A%220101%22%2C%22cityName%22%3A%22%E5%8C%97%E4%BA%AC%22%2C%22startDate%22%3A%222021-07-05%22%2C%22endDate%22%3A%222021-07-06%22%7D; Hm_lpvt_ee805d81eabde6752eabaa82c79a7b7f=1625473925; _dx_captcha_vid=A642A602B5BE69F8A9CC61EC87CDCF08132E5146B04AA2D466F4FF5C9790B610CABA4FCB344D6A67A341D6E66754DC714C2E8063FE8058C85CDD750B1FF927A92DD92D4139ACE33F50AA78CD6F6A35AC; tmcToken=TMCOUU3MUM3NjZBNTZFQTZGQUFCNkQ5QkM3OUJDREU0RUQyNzQ2NjEyQ0EzMDRERTg2NTI1REMxRDQ5QkMyOTVDQw%3D%3D; tokenTime=1625473926503; depCodeName=%E5%8C%97%E4%BA%AC; arrCodeName=%E4%B8%8A%E6%B5%B7; depCodeAndName=%E5%8C%97%E4%BA%AC*PEK*N; arrCodeAndName=%E4%B8%8A%E6%B5%B7*SHA*N";
        con.header("cookie", cookie);
        con.header(
                "user-agent",
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
        Document doc = con.header("Content-Type", "application/json").post();
        Element body = doc.body();
        JSONObject json = JSONObject.parseObject(body.text());
        return json;
    }
}
